﻿@page "/login"
@layout EmptyLayout
@inject IAuthService auth
@inject NavigationManager go
@if (GlobalInfo !=null)
{
    @GlobalInfo.User?.RealName
    @GlobalInfo.Url
}
<BootstrapInput TValue="string" @bind-Value="UserDto.SysName" ShowLabel="true" DisplayText="用户名"></BootstrapInput>
<BootstrapInput TValue="string"  @bind-Value="UserDto.PassWord" ShowLabel="true" DisplayText="密码"></BootstrapInput>
<BootstrapInput TValue="string"  @bind-Value="UserDto.Url" ShowLabel="true" DisplayText="服务器地址"></BootstrapInput>
<Select Items="items" @bind-Value="UserDto.ModuleName"></Select>
<Button OnClick="Log">登录</Button>
<Button OnClick="Louut">注销</Button>
<Button OnClick="Register">注册</Button>
<Button OnClick="Register">访客登录</Button>
@code {
    [CascadingParameter][NotNull]
    public GlobalInfo? GlobalInfo { get; set; }
    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }
    private IEnumerable<SelectedItem>? items=new List<SelectedItem>();
    public Models.Dto.UserDto UserDto { get; set; } = new() { SysName = "1", PassWord = "1", RealName = "" };
    protected override void OnInitialized()
    {
        //var sysmodel = db.Queryable<Models.SystemInfo.Module>().ToList();
        var sysmodel = new List<Module>() { new Module() {  Name="设计器"} };
        if (sysmodel != null)
        {
            items = sysmodel.Select(x => new SelectedItem(x.Name, x.Name));
        }
        base.OnInitialized();
    }
    /// <summary>
    /// 登录
    /// </summary>
    public async void Log()
    {
        var res = await auth.SignInAsync(UserDto);
        if (res.IsSucceeded)
        {
            go.NavigateTo("/",true);
        }
        else
        {
            await ToastService.Success("提示", "登录失败", autoHide: true);
        }
    }
    public async void LogByGuest()
    {
        var res = await auth.SignInAsync(new UserDto() { SysName = "guest", PassWord = "1", RealName = "" });
        if (res.IsSucceeded)
        {
            go.NavigateTo("/", true);
        }
        else
        {
            await ToastService.Success("提示", "登录失败", autoHide: true);
        }
    }
    public async void Louut()
    {
        await auth.SignOutAsync();
        go.NavigateTo("/", true);
    }
    public async void Register()
    {
        var res = await auth.Register(UserDto);
        if (res.IsSucceeded)
        {
            await ToastService.Success("成功", "注册成功", autoHide: true);
        }
        else
        {
            await ToastService.Success("提示", "注册失败", autoHide: true);
        }

    }
}
